// src/stores/blacklist.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'
import request from '../utils/request'

export interface BlacklistUser {
    id: number
    userName: string
    userId: string
    reason: string
    triggerType: string
    startDate: string
    endDate: string
    active: boolean
}

export const useBlacklistStore = defineStore('blacklist', () => {
    const list    = ref<BlacklistUser[]>([])
    const loading = ref(false)

    // 拉取列表
    async function fetchList() {
        loading.value = true
        try {
            const res = await request.get<BlacklistUser[]>('/api/blacklist/list')
            list.value = res.data
        } finally {
            loading.value = false
        }
    }

    // 解封：调用后直接更新本地状态
    async function unban(id: number) {
        await request.put(`/api/blacklist/${id}/unban`)
        const item = list.value.find(i => i.id === id)
        if (item) item.active = false
    }

    // 延长：这里只调用接口，不自动改本地数据
    async function extendBan(id: number, days: number) {
        await request.put(`/api/blacklist/${id}`, { days })
    }

    return { list, loading, fetchList, unban, extendBan }
})
